package net.discard.service;

import com.alibaba.fastjson.JSONObject;
import net.discard.util.HTTPBuilder;
import net.discard.util.HTTPClient;
import org.springframework.stereotype.Service;
import sun.net.www.http.HttpClient;

/**
 * @author Discard
 * @date 15/02/2110:52
 */
@Service
public class WXService {

    public static final String WX_APP_ID = "wx3e7abf7de6658811";
    public static final String WX_APP_SECRET = "f4faf6f9f6b02b2c46f073515cf7adbd";

    /**
     * 根据code获取用户唯一ID
     */
    public String getOpenId(String code){
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        HTTPBuilder builder = new HTTPBuilder()
                .url(String.format(url, WX_APP_ID, WX_APP_SECRET, code))
                .build();
        String res = HTTPClient.get(builder);
        return JSONObject.parseObject(res).getString("openid");
    }
}
